package parser;

import java.io.FileOutputStream;
import java.io.IOException;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.DOTTreeGenerator;
import org.antlr.stringtemplate.StringTemplate;


public class ParserMain {
	public static void main(String args[]){
		FileOutputStream fos = null;
	    TBTLexer lex = null;
        CommonTree t = null;
		
        try {
			lex = new TBTLexer(new ANTLRFileStream("D:\\workspace\\TBT-front-end\\test\\parser.tbt", "UTF8"));
			fos = new FileOutputStream("test\\out.dot");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		// Split the file into lexical atoms
        CommonTokenStream cts = new CommonTokenStream(lex);
        
        TBTParser g = new TBTParser(cts);
		try {
			// Parse and obatin the Abstract Syntax Tree
			t = (CommonTree) g.program().getTree();
		} catch (RecognitionException e1) {
			System.out.println("SYNTAX ERROR: " + e1.getMessage());
			e1.printStackTrace();
		}
        
        // Generate the picture of the AST in .dot format
        DOTTreeGenerator gen = new DOTTreeGenerator();
        StringTemplate st = null;
        try {
        	st = gen.toDOT(t);
            fos.write(st.toString().getBytes());
            fos.close();
        } catch (Exception  e) {
        	System.out.println("COMPILE TIME WARNING: " + e.getMessage());
        	e.printStackTrace();
        }
	}
}
